package org.md.budzik.problems;

import java.math.BigInteger;

/**
 * Obsługa liczb Stirlinga 2 rodzaju.
 * @author Wiezzel
 *
 */
public class StirlingSecondKind extends TwoArgOp {

	public StirlingSecondKind(Expression ex2, Expression ex1) {
		super(ex2, ex1);
	}

	protected BigInteger computeOperation(BigInteger x, BigInteger y) throws ParseException {
		if ((x.compareTo(y) < 0) && !(x.equals(BigInteger.ZERO) && y.equals(BigInteger.ONE)))
			throw new ParseException("Nie istnieje liczba Stirlinga " + x + " po " + y);
		if (x.equals(y))
			return BigInteger.ONE;
		else if (x.equals(BigInteger.ZERO) || y.equals(BigInteger.ZERO))
			return BigInteger.ZERO;
		else if (y.equals(BigInteger.ONE))
			return BigInteger.ONE;
		else
			return computeOperation(x.subtract(BigInteger.ONE), y.subtract(BigInteger.ONE)).add(y.multiply(computeOperation(x.subtract(BigInteger.ONE), y)));
	}

	protected String operationSymbol() {
		return "stir2";
	}

}
